package com.mqy.ssm.contriller;

import com.github.pagehelper.PageInfo;
import com.mqy.ssm.dto.Result;
import com.mqy.ssm.pojo.Book;
import com.mqy.ssm.service.BookService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 13159 //作者
 * @version V1.0 //版本
 * @Project case1
 * @Package com.mqy.ssm.contriller
 * @Description:
 * @Date 2022/3/10 10:31//日期
 * @Motto:放下杂念,只为迎接明天更好的自己
 */

@RestController
@RequestMapping("/book")
public class BookController {

    /*日志*/
    private Logger logger = LoggerFactory.getLogger(BookController.class);

    /*注入的东西*/
    private BookService bookService;

    @Autowired
    public void setBookService(BookService bookService) {

        this.bookService = bookService;
    }

    @GetMapping("/findID/{id}")
    public Result findID(@PathVariable Long id) {
        logger.error("根据id查询数据的方法执行了{}", id);
        Book id1 = bookService.findID(id);
        return Result.setOK(id1);

    }

    @GetMapping("/findall")
    public Result findAll(@RequestParam(defaultValue = "2") int pageSize, @RequestParam(defaultValue = "1") int pageNum) {

        /*日志*/
        logger.error("分页查询数据的方法执行了,当前查询的是第{}页,每页要{}条", pageNum,pageSize);

        PageInfo<Book> all = bookService.findAll(pageSize, pageNum);
        return Result.setOK(all);

    }

    @PostMapping("/bookAdd")
    public Result bookAdd(@RequestBody Book book) {
        bookService.bookAdd(book);
        return Result.setOK();
    }

    @GetMapping("/bookremove/{id}")
    public Result boodremove(@PathVariable Long id) {
        bookService.boodremove(id);

        return Result.setOK();
    }

    @PostMapping("/bookupdate")
    public Result bookupdate(@RequestBody Book book) {
        bookService.bookupdate(book);
        return Result.setOK();
    }

}
